/**
 * \class CWorkSpaces
 * \brief This is a core class to make spaces efficently used.
 * \version 0.3.3
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2008/8/5
 */
#ifndef CWorkSpacesH
#define CWorkSpacesH
//-----------------------------------------------------------------
#include <iostream>

#include "random/CRandom.h"

#include "CChord.h"
#include "CSpaceProxy.h"
#include "CBulkSpaceProxy.h"
//-----------------------------------------------------------------
using namespace std;
//-----------------------------------------------------------------
class CWorkSpaces
{
private:
	string host;
	int port;

	string _service; // service type
	string _instance; // real instance of service

	bool auto_clean;

	// check if coord is alive
	int success;

protected:
	bool use_other_topology;
	CDHT *_topology;

	bool use_bulk;
	CSpaceProxy *space;
	CBulkSpaceProxy *bulk_space;

	int _clean(CServiceInfo &found_space);

	void initialize();
	void finalize();
	
public:
	CWorkSpaces(char *dip, int dport);
	CWorkSpaces(char *dip, int dport, char *service, char *instance);
	CWorkSpaces(CDHT *top);
	virtual ~CWorkSpaces();

	void service(char *s);
	const char *service();
	void instance(char *i);
	const char *instance();
	CDHT *topology();

	bool alive();

	int bulk(int limit);
	int flush();
	long size();
	long size(string &space_key);
	void autoclean(bool is_clean);
	int clean(string &key);
	int cleanup();

	int write(string &space_key, pair<string,string> &kv);
	int write(pair<string,string> &kv);
	int read(string &space_key, pair<string,string> &kv);
	int read(pair<string,string> &kv);
	int readIfExists(string &space_key, pair<string,string> &kv);
	int readIfExists(pair<string,string> &kv);
	int take(string &space_key, pair<string,string> &kv);
	int take(pair<string,string> &kv);
	int takeIfExists(string &space_key, pair<string,string> &kv);
	int takeIfExists(pair<string,string> &kv);
};
//-----------------------------------------------------------------
#endif
